package tx.bj12320.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.org.rapid_framework.beanutils.BeanUtils;
import cn.org.rapid_framework.web.scope.Flash;

import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.ModelDriven;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import tx.bj12320.model.*;
import tx.bj12320.dao.*;
import tx.bj12320.service.*;
import tx.bj12320.vo.query.*;


public class DataDictAction extends BaseStruts2Action implements Preparable,ModelDriven{
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/pages/DataDict/query.jsp";
	protected static final String LIST_JSP= "/pages/DataDict/list.jsp";
	protected static final String CREATE_JSP = "/pages/DataDict/create.jsp";
	protected static final String EDIT_JSP = "/pages/DataDict/edit.jsp";
	protected static final String SHOW_JSP = "/pages/DataDict/show.jsp";
	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/pages/DataDict/list.do";

	private DataDictManager dataDictManager;

	private DataDict dataDict;
	Long id = null;
	private String[] items;

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			dataDict = new DataDict();
		} else {
			dataDict = (DataDict)dataDictManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setDataDictManager(DataDictManager manager) {
		this.dataDictManager = manager;
	}

	public Object getModel() {
		return dataDict;
	}

	public void setDdId(Long val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		DataDictQuery query = newQuery(DataDictQuery.class,DEFAULT_SORT_COLUMNS);

		Page page = dataDictManager.findPage(query);
		savePage(page,query);
		return LIST_JSP;
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		dataDictManager.save(dataDict);
		Flash.current().success(CREATED_SUCCESS); //存放在Flash中的数据,在下一次http请求中仍然可以读取数据,error()用于显示错误消息
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		dataDictManager.update(this.dataDict);
		Flash.current().success(UPDATE_SUCCESS);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		for(int i = 0; i < items.length; i++) {
			Hashtable params = HttpUtils.parseQueryString(items[i]);
			Long id = new Long((String)params.get("ddId"));
			dataDictManager.removeById(id);
		}
		Flash.current().success(DELETE_SUCCESS);
		return LIST_ACTION;
	}

	public String tree() {
		DataDictQuery query = newQuery(DataDictQuery.class,DEFAULT_SORT_COLUMNS);
		query.setPageSize(1);
		query.setDdType(dataDict.getDdType());
		query.setDdLevel(new Long(0));
		Page page = dataDictManager.findPage(query);

		DataDict rootDd=(DataDict)page.getResult().get(0);
		List<DataDict> dds=dataDictManager.tree(rootDd);

		StringBuffer sb=new StringBuffer();
		gentree(dds, sb);

		getRequest().setAttribute("tree", sb.toString());
		return "/tszb/tree.jsp";
	}

	private void gentree(List<DataDict> dds, StringBuffer sb) {
		if(dds==null||dds.size()==0) return;
		for (DataDict dataDict : dds) {
			sb.append("<li id='"+dataDict.getDdValue()+"' ");
			if(dataDict.getChildren()!=null) sb.append(" state='closed' ");
			sb.append(">");
			sb.append("<span >"+dataDict.getDdName()+"</span>");
			sb.append("<ul>");
			gentree(dataDict.getChildren(),sb);
			sb.append("</ul>");
			sb.append("</li>");

		}
	}

}
